﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace SnapLib
{
   public class ForeignComboBox : ForeignControl
   {
      public ForeignComboBox(IntPtr parentWnd, uint ctrlId) :
         base(parentWnd, ctrlId)
      {
      }

      public string SelectedText
      {
         get
         {
            return GetText(SelectedIndex);
         }
         set
         {
         }
      }

      public string GetText(uint itemIndex)
      {
         StringBuilder sb = new StringBuilder(512);
         Win32.SendMessage(m_hwnd, WindowsMessage.CB_GETLBTEXT, itemIndex, sb);
         return sb.ToString();
      }

      public uint GetItemData(uint itemIndex)
      {
         return Win32.SendMessage(m_hwnd, WindowsMessage.CB_GETITEMDATA, itemIndex, 0);
      }

      public void SetItemData(uint itemIndex, uint data)
      {
         Win32.SendMessage(m_hwnd, WindowsMessage.CB_SETITEMDATA, itemIndex, data);
      }

      public int ItemCount
      {
         get
         {
            return (int)Win32.SendMessage(m_hwnd, WindowsMessage.CB_GETCOUNT, 0, 0);
         }
      }

      public uint SelectedIndex
      {
         get
         {
            return Win32.SendMessage(m_hwnd, WindowsMessage.CB_GETCURSEL, 0, 0);
         }
         set
         {
            Win32.SendMessage(m_hwnd, WindowsMessage.CB_SETCURSEL, value, 0);
         }
      }

      public bool DroppedDown
      {
         get
         {
            return Win32.SendMessage(m_hwnd, WindowsMessage.CB_GETDROPPEDSTATE, 0, 0) == 0 ? false : true;
         }
         set
         {
            Win32.SendMessage(m_hwnd, WindowsMessage.CB_SHOWDROPDOWN, (uint)(value ? 1 : 0), 0);
         }
      }

      public uint MaxDropDownItems
      {
         get
         {
            return Win32.SendMessage(m_hwnd, WindowsMessage.CB_GETMINVISIBLE, 0, 0);
         }
         set
         {
            Win32.SendMessage(m_hwnd, WindowsMessage.CB_SETMINVISIBLE, value, 0);
         }
      }

      public IEnumerable<string> Items
      {
         get
         {
            for (uint i = 0; i < ItemCount; i++)
               yield return GetItemText(i);
         }
      }

      public string GetItemText(uint index)
      {
         uint len = Win32.SendMessage(m_hwnd, WindowsMessage.CB_GETLBTEXTLEN, index, 0);
         StringBuilder sb = new StringBuilder((int)len + 1);
         Win32.SendMessage(m_hwnd, WindowsMessage.CB_GETLBTEXT, index, sb);
         return sb.ToString();
      }

      new public string Text
      {
         get
         {
            StringBuilder sb = new StringBuilder(256);
            Win32.SendMessage(m_hwnd, WindowsMessage.WM_GETTEXT, 256, sb);
            return sb.ToString();
         }
         set
         {
            SendMessage(WindowsMessage.WM_SETTEXT, 0, new StringBuilder(value));
         }
      }
   }
}
